<?php


namespace app\api\controller;


use addons\recharge\library\Order;
use app\common\controller\Api;
use think\Db;
use think\Exception;

class Finance extends Api
{
    protected $noNeedLogin = ['setting','aagg'];
    protected $noNeedRight = ['*'];

    protected static $limit = 10;

    public function aagg()
    {
        $id = $this->request->get('id');
        \addons\recharge\model\Order::rechargeBalance($id);
        echo  's';
    }

    /**
     * 财务设置
     * @return string
     */
    public function setting()
    {
        $config = get_addon_config('recharge');
        $moneyList = [];
        foreach ($config['moneylist'] as $index => $item) {
            $moneyList[] = ['value' => $item, 'text' => $index, 'default' => $item === $config['defaultmoney']];
        }
        $paytypeList = [];
        foreach (explode(',', $config['paytypelist']) as $index => $item) {
            $paytypeList[] = ['value' => $item, 'image' => '/assets/addons/recharge/img/' . $item . '.png', 'default' => $item === $config['defaultpaytype']];
        }

        $this->success("返回成功",[
            //充值
            'recharge' => [
                'config' => $config,
                'moneyList' => $moneyList,
                'paytypeList' => $paytypeList
            ],
            //提现
            'withdraw' => [
                'config' => $config = get_addon_config('withdraw')
            ],
        ]);

    }

    /**
     * 充值
     *
     */
    public function recharge()
    {
        $image = $this->request->post('image'); //凭证
        $money = $this->request->post('money');
        $paytype = $this->request->post('paytype');

        if ($money <= 0) {
            $this->error('充值金额不正确');
        }

        $config = get_addon_config('recharge');
        if (isset($config['minmoney']) && $money < $config['minmoney']) {
            $this->error('充值金额不能低于' . $config['minmoney'] . '元');
        }

        if ($image == "") {
            $this->error('请上传凭证');
        }

        try {
            Order::submit1($money, $paytype ? $paytype : 'wechat', $image);
        } catch (Exception $e) {
            $this->error($e->getMessage());
        }

        $this->success("充值申请成功！请等待后台审核！");
    }

    /**
     * 充值记录
     *
     */
    public function rechargeList()
    {
        $page = $this->request->get("page");
        $status = $this->request->get("status") ? $this->request->get("status") : 0;

        $where = ['user_id' => $this->auth->id];
        if($status != 0) {

        }
        switch ($status) {
            case 1://审核中
                $where['status'] = 'created';
                break;
            case 2://同意
                $where['status'] = 'paid';
                break;
            case 3://拒绝
                $where['status'] = 'expired';
                break;
            default:
                break;
        }
        $list = \addons\recharge\model\Order::where($where)
            ->order('id desc')
            ->paginate(self::$limit,false, ['page'=> $page ? $page : 1]);

        $result = array("total" => $list->total(), "rows" => $list->items());

        $this->success('请求成功',$result);
    }

    /**
     * 提现
     *
     */
    public function withdraw()
    {
        $money = $this->request->post('money');
        $account = $this->request->post('account');
        $name = $this->request->post('name');
        $posswoed = $this->request->post('posswoed');

        $type = 'alipay';

        if ($money <= 0) {
            $this->error('提现金额不正确');
        }
        if ($money > $this->auth->money) {
            $this->error('提现金额超出可提现额度');
        }
        if (!$account) {
            $this->error("提现账户不能为空");
        }
        if (!$name) {
            $this->error("真实姓名不能为空");
        }

        $config = get_addon_config('withdraw');
        if (isset($config['minmoney']) && $money < $config['minmoney']) {
            $this->error('提现金额不能低于' . $config['minmoney'] . '元');
        }

        if ($config['monthlimit']) {
            $count = \addons\withdraw\model\Withdraw::where('user_id', $this->auth->id)->whereTime('createtime', 'month')->count();
            if ($count >= $config['monthlimit']) {
                $this->error("已达到本月最大可提现次数");
            }
        }
        if ($posswoed != $this->auth->payment_code) {
            $this->error('支付密码不正确');
        }
        Db::startTrans();
        try {
            $data = [
                'orderid' => date("YmdHis") . sprintf("%08d", $this->auth->id) . mt_rand(1000, 9999),
                'user_id' => $this->auth->id,
                'money'   => $money,
                'type'    => $type,
                'account' => $account,
                'name'    => $name,
            ];
            \addons\withdraw\model\Withdraw::create($data);
            \app\common\model\User::money(-$money, $this->auth->id, "提现");
            Db::commit();
        }catch (Exception $e) {
            Db::rollback();
            $this->error($e->getMessage());
        }

        $this->success("提现申请成功！请等待后台审核！");
    }

    /**
     * 提现记录
     *
     */
    public function withdrawList()
    {
        $page = $this->request->get("page");

        $list = \addons\withdraw\model\Withdraw::where(['user_id' => $this->auth->id])
            ->order('id desc')
            ->paginate(self::$limit,false, ['page'=> $page ? $page : 1]);

        $result = array("total" => $list->total(), "rows" => $list->items());

        $this->success('请求成功',$result);
    }
}